package work.managercenter;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import utils.MyUtils;

public class TestGetComplexPhone {
	public static void main(String[] args) {
		try {
			int numberOfThreads = 100;

			Thread[] threads = new Thread[numberOfThreads];

			for (int i = 0; i < threads.length; i++) {
				threads[i] = new Thread(new Runnable() {

					@Override
					public void run() {
						try {
							getComplexPhone("1830");
						} catch (Exception e) {
							e.printStackTrace(System.out);
						}
					}
				});
			}

			for (int i = 0; i < threads.length; i++) {
				threads[i].start();
			}

		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	private static void getComplexPhone(String complexId) throws Exception {
		String urlAddress =  "http://stg-aptrest1.internetbrands.com:8080/services/managercenter/v1/getComplexPhoneNumber";

		String params = "nocache=" + java.net.URLEncoder.encode("true","UTF-8") +
				"&complexId=" + java.net.URLEncoder.encode(complexId,"UTF-8") +
				"&format=json";
		byte[] bytes = params.getBytes("UTF-8");
		URL url = new URL(urlAddress);
		HttpURLConnection conn1 = (HttpURLConnection) url.openConnection();
		conn1.setRequestMethod("GET");
		conn1.setRequestProperty("Accept", "application/json");
		conn1.setRequestProperty("Content-Length", String.valueOf(bytes.length));
		conn1.setDoOutput(true);
		OutputStream outputStream = conn1.getOutputStream();
		outputStream.write(bytes);
		BufferedReader br = new BufferedReader(new InputStreamReader((conn1.getInputStream())));

		String output;
		StringBuilder json = new StringBuilder();

		while ((output = br.readLine()) != null) {
			json.append(output).append(MyUtils.EOL);
		}

		conn1.disconnect();

		System.out.println("json: " + json);
	}
}
